<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_kaoshi/model')->方法名();
*/
namespace jy_videocall;
defined('BY_JYA') or exit('error');
class kefu extends \plugin{
	public function gettcp($tcpid=0,$tomid=0,$workerid=0,$isfan=0){
		if(!$tcpid){
			if($tomid==$_SESSION['uid']){
				exi('不能自己跟自己对话','error');
			}
			$item=pdo_get('jy_videocall_tcp',array('tomid'=>$isfan?$_SESSION['uid']:$tomid,'workerid'=>$workerid,'mid'=>$isfan?$tomid:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$item['id']){
				$item=pdo_get('jy_videocall_tcp',array('mid'=>$tomid,'workerid'=>$workerid,'tomid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			}
			if(!$item['id']){
				$item=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$isfan?$tomid:$_SESSION['uid'],
					'tomid'=>$isfan?$_SESSION['uid']:$tomid,
					'workerid'=>$workerid,
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_videocall_tcp',$item);
				$item['id']=pdo_insertid();
			}
		}else{
			$item=pdo_get('jy_videocall_tcp',array('uniacid'=>$_SESSION['uniacid'],'id'=>$tcpid));
		}
		return $item;
	}
	public function videocall($tomid=0,$endcall=0,$status=0){
		$item=pdo_get('jy_videocall_tcp',array('tomid'=>$tomid,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$item['id']){
			$item=pdo_get('jy_videocall_tcp',array('mid'=>$tomid,'tomid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$item['id']){
			return '';
		}
		//挂断
		if($endcall){
			pdo_update('jy_videocall_tcp',array('status'=>0,'endtime'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$status=0;
		}else{
			//拨号中
			if($status==1){
				pdo_update('jy_videocall_tcp',array('status'=>1,'callmid'=>$_SESSION['uid']),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$status=1;
			}
			//接听
			if($status==2 && $item['callmid']!=$_SESSION['uid']){
				pdo_update('jy_videocall_tcp',array('status'=>2,'calltime'=>time()),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$status=2;
			}
		}
		return $status;
	}
}
?>